import java.sql.*;

import org.opengauss.Driver;

public class pg_prepared_statement_case0039 {
	public static void main(String[] args) throws SQLException {
		System.out.println(Driver.getGSVersion());
		String url = "jdbc:opengauss://" + args[0] + ":" + args[1] + "/" + args[2];
        String username = args[3];
        String password = args[4];
		String sql;
        int conn_num = Integer.parseInt(args[5]);

        System.out.println("----新建连接1个主连接----");
        Connection conn_pri = GetConnection(username, password, url);

		System.out.println("----新建连接" + args[5] + "个测试连接----");
		Connection[] conn = new Connection[conn_num];
		for(int i=0; i<conn_num; i++) {
			conn[i] = GetConnection(username, password, url);
		}
		
		try {
			System.out.println("----主连接建表----");
			sql = "drop table if exists t_prepare_39;\r\n" +
			    "create table t_prepare_39(id int, name text);";
			execute(sql, conn_pri);

			System.out.println("----每个测试连接执行预编译SQL语句----");
			sql = "insert into t_prepare_39 values (?, ?);";
			for(int i=0; i<conn_num; i++) {
                PreparedStatement pstmt = conn[i].prepareStatement(sql);
                for (int j = 1; j < 10; ++j) {
                    pstmt.setInt(1, j);
                    pstmt.setString(2, "Name " + j);
                    pstmt.executeUpdate();
                }
                pstmt.close();
			}

			System.out.println("----主连接查询----");
			sql = "select * from pg_prepared_statement(0);";
			QueryData(sql, conn_pri);
			sql = "select count(*) from pg_prepared_statement(0);";
			QueryData(sql, conn_pri);

			System.out.println("----关闭测试连接----");
			for(int i=0; i<conn_num; i++) {
				conn[i].close();
			}

			System.out.println("----主连接再次查询----");
			QueryData("select pg_sleep(1);", conn_pri);
			sql = "select * from pg_prepared_statement(0);";
			QueryData(sql, conn_pri);
			sql = "select count(*) from pg_prepared_statement(0);";
			QueryData(sql, conn_pri);

			System.out.println("----删除表----");
			sql = "drop table if exists t_prepare_39 cascade;";
			executeUpdate(sql, conn_pri);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("----关闭连接----");
			for(int i=0; i<conn_num; i++) {
				if (conn[i] != null) {conn[i].close();}
			}
			if (conn_pri != null) {conn_pri.close();}
		}
	}

	public static Connection GetConnection(String username, String passwd, String sourceURL) {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(sourceURL, username, passwd);
			System.out.println(sourceURL);
			System.out.println("Connection succeed");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return conn;
	}
	
	public static void QueryData(String sql, Connection conn) {
		try {
			System.out.println(sql) ;
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			int cols = rsmd.getColumnCount();
			System.out.println("----查询结果----");
			for(int i=1; i<cols+1; i++) {
			    if (i==cols){
					System.out.println(rsmd.getColumnName(i));
				} else {
					System.out.print(rsmd.getColumnName(i) +"|");
				}
			}
			while (rs.next()) {
				for(int i=1; i<cols+1; i++) {
					if (i==cols){
						System.out.println(rs.getString(i));
					} else {
						System.out.print(rs.getString(i) +"|");
					}
				}
			}
            ps.close();   
        } catch (SQLException e) {
            System.out.println("ERROR:" + e.getMessage());
            e.printStackTrace();
		}
	}
	
	public static void executeUpdate(String sql, Connection conn) {
		try {
			System.out.println(sql) ;
			Statement stmt = conn.createStatement();
			int rc = stmt.executeUpdate(sql);
			if (rc>=0) {
				System.out.println(rc);
				System.out.println("SQL SUCCESS");
			}
		} catch (SQLException e) {
            System.out.println("ERROR:" + e.getMessage());
            e.printStackTrace();
		}
	}
	
	public static void execute(String sql, Connection conn) {
		try {
			System.out.println(sql) ;
			Statement stmt = conn.createStatement();
			Boolean rb = stmt.execute(sql);
			System.out.println(rb);
			System.out.println("SQL SUCCESS");
		} catch (SQLException e) {
            System.out.println("ERROR:" + e.getMessage());
            e.printStackTrace();
		}
	}
}